package 单表查询;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Demo01 {
    public static void main(String[] args) throws Exception {
//        1.加载驱动com.mysql.jdbc.Driver
        Class.forName("com.mysql.jdbc.Driver");
//        2.使用驱动管理器（DriverManager）连接数据库              ？是后面要加参数&支持中文&xx连接关闭&服务器通用时区
        String url = "jdbc:mysql://localhost/db_jdbc_demo?characterEncoding=utf8&useSSL=false&serverTimezone=UTC";
        String user = "root";
        String pass = "JJ20021686361520jun";
        Connection conn = DriverManager.getConnection(url, user, pass);
//        3.书写sql语句并执行
        String sql = "SELECT * FROM t_user";
//        4.创建执行SQL的对象
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
//        5.从rs中取出结果
        while (rs.next()){
            String username = rs.getString("username");
            String fullname = rs.getString("fullname");
            System.out.println(username + "," + fullname);
        }
//        6.关闭连接
        rs.close();
        stmt.close();
        conn.close();
    }
}
